package service;

import beans.User;
import dao.BoardFileDAO;
import dao.UserDAO;
import server.whiteboard.WbResponse;
import server.whiteboard.WhiteBoardManager;
import server.whiteboard.WhiteBoardRequestThread;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class BoardService {

    public static void connectBoard(long user_id, long board_id, String address, WhiteBoardRequestThread.RequestContext rct) {
        User user = UserDAO.ME.getById(user_id);
        BoardFileDAO.ME.addOnlineMember(board_id, user.getAccount(), address);
        //向所有在线成员发送更新在线成员列表
        Set<String> addressList = BoardFileDAO.ME.getOnlineMembersAddress(board_id);
        WbResponse onlineMembers = new WbResponse(WbResponse.FLUSH_MEMBERS_CODE, BoardFileDAO.ME.getOnlineMembers(board_id));
        for (String memberAddress : addressList) {
            rct.sendResponse(onlineMembers, memberAddress);
        }
        //向用户发送已有的修改
        List<String> shapeList = BoardFileDAO.ME.getBoardShapeList(board_id);
        int shapeSize = shapeList.size();
        List<String> onceSendShapeList = new ArrayList<>();
        for(int i=0, toIndex=1000;i<shapeSize;i=i+1000,toIndex=toIndex+1000) {
            if (i + 1000 > shapeSize) {
                toIndex = shapeSize;
            }
            onceSendShapeList = shapeList.subList(i, toIndex);
            WbResponse wbResponse = new WbResponse(WbResponse.LOAD_BOARD_CODE, onceSendShapeList);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            rct.sendResponse(wbResponse, address);
        }
    }

    public static void updateBoard(long board_id, List<String> updateShapeList, WhiteBoardRequestThread.RequestContext rct) {
        if (updateShapeList.isEmpty()) {
            return;
        }
        BoardFileDAO.ME.addBoardShape(board_id, updateShapeList);
        Set<String> addressList = BoardFileDAO.ME.getOnlineMembersAddress(board_id);
        int shapeSize = updateShapeList.size();
        List<String> onceSendShapeList = new ArrayList<>();
        for(int i=0, toIndex=1000;i<shapeSize;i=i+1000,toIndex=toIndex+1000) {
            if (i + 1000 > shapeSize) {
                toIndex = shapeSize;
            }
            onceSendShapeList = updateShapeList.subList(i, toIndex);
            WbResponse wbResponse = new WbResponse(WbResponse.UPDATE_BOARD_CODE, onceSendShapeList);
            for (String memberAddress : addressList) {
                if (!memberAddress.equals(rct.getAddress())) {
                    rct.sendResponse(wbResponse, memberAddress);
                }
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveBoard(long board_id, WhiteBoardRequestThread.RequestContext rct) {
        BoardFileDAO.ME.saveBoardContent(board_id);
        WbResponse wbResponse = new WbResponse(WbResponse.MSG_CODE, "白板内容已保存至云端");
        rct.sendResponse(wbResponse, rct.getAddress());
    }

    public static void clearBoard(long board_id, WhiteBoardRequestThread.RequestContext rct) {
        BoardFileDAO.ME.clearCacheBoard(board_id);
        Set<String> addressList = BoardFileDAO.ME.getOnlineMembersAddress(board_id);
        WbResponse wbResponse = new WbResponse(WbResponse.CLEAR_BOARD_CODE, "管理员清除了白板");
        for (String memberAddress : addressList) {
            if (!memberAddress.equals(rct.getAddress())) {
                rct.sendResponse(wbResponse, memberAddress);
            }
        }
    }

    public static void disconnectBoard(long user_id, long board_id, WhiteBoardRequestThread.RequestContext rct) {
        User user = UserDAO.ME.getById(user_id);
        BoardFileDAO.ME.deleteOnlineMember(board_id, user.getAccount(), rct.getAddress());
        //向所有在线成员发送更新在线成员列表
        Set<String> members = BoardFileDAO.ME.getOnlineMembers(board_id);
        if (!members.isEmpty()) {
            Set<String> addressList = BoardFileDAO.ME.getOnlineMembersAddress(board_id);
            WbResponse onlineMembers = new WbResponse(WbResponse.FLUSH_MEMBERS_CODE, members);
            for (String memberAddress : addressList) {
                rct.sendResponse(onlineMembers, memberAddress);
            }
        }
    }

    public static void broadcastMsg(long board_id, String msg, WhiteBoardRequestThread.RequestContext rct) {
        Set<String> addressList = BoardFileDAO.ME.getOnlineMembersAddress(board_id);
        WbResponse wbResponse = new WbResponse(WbResponse.CHAT_MESSAGE_CODE, msg);
        for (String memberAddress : addressList) {
            if (!memberAddress.equals(rct.getAddress())) {
                rct.sendResponse(wbResponse, memberAddress);
            }
        }
    }
}
